<?php

class FieldDefinition
{
	private $type;
	private $label;
	private $flag_having;
	// tableau des valeurs possibles pour TYPE_OPTIONS_OR et TYPE_OPTIONS_XOR
	private $options = null;
	private $value = null;
	private $value2 = null; // 2ème valeur pour les champs min/max
	
	function __construct($type, $label, $flag_having = False)
	{
		$this->type = $type;
		$this->label = $label;
		$this->flag_having = $flag_having;
	}
	
	function add_option($value, $label) 
	{
		if ($this->type != TYPE_OPTIONS_OR and $this->type != TYPE_OPTIONS_XOR) {
			throw new Exception("FieldDefinition::add_option: field must be 'TYPE_OPTIONS_OR' or 'TYPE_OPTIONS_XOR'");
		}
		if ($this->options == null) {
			$this->options = array();
		}
		$this->options[] = array('value' => $value, 
		                         'label' => $label);
	}
	
	function show_field($field)
	{
		switch ($this->type) {
			case TYPE_BOOL:
				echo '<span style="float: left;">
					<input type="radio" name="'.$field.'" value="1" id="field_'.$field.'_1" '.($this->value === 1 ? 'checked="checked"' : '').' />
					<label for="field_'.$field.'_1">Oui</label>&nbsp;
					<input type="radio" name="'.$field.'" value="0" id="field_'.$field.'_0"  '.($this->value === 0 ? 'checked="checked"' : '').' />
					<label for="field_'.$field.'_0">Non</label>
				</span>';
				$this->clear_button('#field_'.$field.'_0, #field_'.$field.'_1');
				break;
				
			case TYPE_STRING:
				echo '<input type="text" name="'.$field.'" id="field_'.$field.'" value="'.$this->value.'" style="float: left;"/>';
				$this->clear_button('#field_'.$field);
				break;
				
			case TYPE_INT:
				echo '<span style="float: left;">entre
				<input type="text" name="'.$field.'_min" size="4" class="js-numeric" id="field_'.$field.'_min" value="'.$this->value.'" /> et
				<input type="text" name="'.$field.'_max" size="4" class="js-numeric" id="field_'.$field.'_max" value="'.$this->value2.' "/>
				</span>';
				$this->clear_button('#field_'.$field.'_min, #field_'.$field.'_max');
				break;
				
			case TYPE_DATE:
				echo '<span style="float: left;">entre
				<input type="text" name="'.$field.'_min" size="8" class="js-date" id="field_'.$field.'_min" value="'.$this->value.'" /> et
				<input type="text" name="'.$field.'_max" size="8" class="js-date" id="field_'.$field.'_max" value="'.$this->value2.'" />
				</span>';
				$this->clear_button('#field_'.$field.'_min, #field_'.$field.'_max');
				break;
				
			case TYPE_OPTIONS_OR:
				foreach ($this->options as $option)
				{
					echo '<input type="checkbox" name="'.$field.'[]" value="'.$option['value'].'" '.(is_array($this->value) && in_array($option['value'], $this->value) ? 'checked="checked"' : '').' /> '.$option['label'].'<br />';
				}
				break;
			
			case TYPE_OPTIONS_XOR:
				echo '<select name="'.$field.'" id="field_'.$field.'" style="float: left;">';
				echo '<option value=""></value>';
				foreach ($this->options as $option)
				{
					$check = $this->value == $option['value'] ? 'selected="selected"' : '';
					echo '<option value="'.$option['value'].'" '.$check.'>'.$option['label'].'</value>';
				}
				echo '</select>';
				$this->clear_button('#field_'.$field);
				break;
				
			default:
				throw new Exception('FieldDefinition::show_field: unhandled field type');
		}
	}
	
	function use_having()
	{
		return $this->flag_having;
	}
	
	function get_type()
	{
		return $this->type;
	}
	function get_label()
	{
		return $this->label;
	}
	function &get_options()
	{
		return $this->options;
	}
	
	/**
	 * @param value: valeur recherchée
	 * @param value2: 2ème valeur nécessaire pour les champs min max
	 */
	function set_value($value, $value2 = null)
	{
		$this->value = $value;
		$this->value2 = $value2;
	}
	
	/**
	 * Afficher un bouton d'effacement d'un champ
	 * @param jquery_selector: sélecteur JS des champs à effacer
	 */
	private function clear_button($jquery_selector)
	{
		if ($this->type == TYPE_BOOL) {
			$js = '$(\''.$jquery_selector.'\').attr(\'checked\', false);';
		}
		else {
			$js = '$(\''.$jquery_selector.'\').val(\'\');';
		}
		?>
		<a href="javascript:void(0)" onclick="<?php echo $js ?>"
			class="ui-state-default ui-corner-all" style="float: left; margin-left: 4px;" title="Effacer <?php echo $this->label ?>">
			<span class="ui-icon ui-icon-trash"></span>
		</a>
		<?php
	}
}
